<template>
  <div class="app-container">
    <el-card>
      <el-form ref="formData" :inline="true" :model="carSearchFormData" label-width="80px">
        <el-row :gutter="60">
          <el-col :span="8">
            <el-form-item label="车型编号:" prop="id">
              <el-input v-model="carSearchFormData.id" clearable placeholder="请输入车型编号" style="width:260px" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应载重量:" prop="allowableLoad">
              <el-select v-model="carSearchFormData.allowableLoad" clearable placeholder="请选择应载重量" style="width:260px">
                <el-option
                  v-for="(item,index) in allowableLoadList"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应载体积:" prop="allowableVolume">
              <el-select v-model="carSearchFormData.allowableVolume" clearable placeholder="请选择应载体积" style="width:260px">
                <el-option
                  v-for="(item,index) in allowableVolumeList"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车型类型:" prop="name">
              <el-input v-model="carSearchFormData.name" clearable placeholder="请输入车辆类型" style="width:260px" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button class="button" type="primary" @click="search">搜索</el-button>
              <el-button class="button" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row></el-form>
    </el-card>

    <el-card>
      <el-button type="primary" @click="handleAdd">新增车型</el-button>
      <!-- 表格 -->
      <el-table v-loading="loading" stripe :data="carTypList" style="width: 100%" :header-cell-style="{ background: 'rgba(250,252,255,1)' }">
        <el-table-column label="序号" width="50px" align="center">
          <template #default="slot">
            {{ (carSearchFormData.page - 1) * carSearchFormData.pageSize + slot.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="车型编号" align="center" width="180" />
        <el-table-column prop="name" label="车辆类型" align="center" width="180" />
        <el-table-column prop="num" label="车辆数量" align="center" />
        <el-table-column prop="allowableLoad" label="应载重量(吨)" align="center" />
        <el-table-column prop="allowableVolume" label="应载体积(立方米)" width="180" align="center" />
        <el-table-column prop="measureLong" label="长(米)" align="center" />
        <el-table-column prop="measureWidth" label="宽(米)" align="center" />
        <el-table-column prop="measureHigh" label="高(米)" align="center" />
        <el-table-column label="操作" width="110" align="center">
          <template #default="{row}">
            <el-button
              size="mini"
              type="text"
              style="color:#419eff"
              @click="handleUpdate(row)"
            >编辑</el-button>
            <div class="line" />
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        align="center"
        :page-sizes="[10,20,30,40,50]"
        :page-size="carSearchFormData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page.sync="carSearchFormData.page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 新增和修改车型信息对话框 -->
    <el-dialog :visible.sync="open" :title="title" append-to-body width="600px">
      <el-form ref="form" style="padding-left:60px" round :model="formDlog" :rules="rulesDlog">
        <el-form-item label="车辆类型" prop="name" label-width="78px">
          <el-input v-model="formDlog.name" placeholder="请输入车辆类型" style="width:380px" />
        </el-form-item>
        <el-form-item label="应载重量" prop="allowableLoad" label-width="78px">
          <el-input v-model.number="formDlog.allowableLoad" placeholder="请输入车型载重" style="width:380px">
            <i slot="suffix" class="dw">吨</i>
          </el-input>
        </el-form-item>
        <el-form-item label="应载体积" prop="allowableVolume" label-width="78px">
          <el-input v-model.number="formDlog.allowableVolume" placeholder="请输入车型体积" style="width:380px">
            <i slot="suffix" class="dw">立方</i>
          </el-input>
        </el-form-item>
        <el-form-item label="长" label-width="78px" prop="measureLong">
          <el-input v-model="formDlog.measureLong" placeholder="" style="width:380px">
            <i slot="suffix" class="dw">米</i>
          </el-input>
        </el-form-item>
        <el-form-item label="宽" label-width="78px" prop="measureWidth">
          <el-input v-model="formDlog.measureWidth" placeholder="" style="width:380px">
            <i slot="suffix" class="dw">米</i>
          </el-input>
        </el-form-item>
        <el-form-item label="高" label-width="78px" prop="measureHigh">
          <el-input v-model="formDlog.measureHigh" placeholder="" style="width:380px">
            <i slot="suffix" class="dw">米</i>
          </el-input>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { getCarTypePage, addCarType, updataCarType, delCarType } from '@/api/car'
export default {
  data() {
    return {
      // 搜索表单,应载重量数组
      allowableLoadList: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<1.8（吨）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '1.8-6（吨）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '6-14（吨）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '14-30（吨）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '30-50（吨）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '50-100（吨）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '100>（吨）'
        }
      ],
      // 搜索表单,应载体积数组
      allowableVolumeList: [
        {
          value: 'RANGE_LEVEL_1',
          name: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          name: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          name: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          name: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          name: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          name: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          name: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          name: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          name: '150>（m³）'
        }
      ],
      // 搜索关键字
      searchkey: false,
      carTypList: [],
      carSearchFormData:
      {
        page: 1,
        pageSize: 10
      },
      index: 0,
      id: '',
      loading: false,
      total: 0,
      title: '',
      open: false,

      formDlog: {
        name: '',
        allowableLoad: '',
        allowableVolume: '',
        measureLong: '',
        measureWidth: '',
        measureHigh: ''
      },

      rulesDlog: {
        name: [
          { required: true, message: '车型名称不能为空', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '应载重量不能为空', trigger: 'blur' },
          { min: 1, max: 100, type: 'number', message: '只可输入1-100之间的整数', trigger: 'blur' }
        ],
        allowableVolume: [{ required: true, message: '应载体积不能为空', trigger: 'blur' },
          { min: 1, max: 100, type: 'number', message: '只可输入1-100之间的整数', trigger: 'blur' }]
      },
      options: []
    }
  },
  created() {
    this.getCarList()
  },
  methods: {
    async getCarList() {
      const res = await getCarTypePage(this.carSearchFormData)
      console.log(res)
      this.carTypList = res.items
      this.total = res.counts * 1
      this.loading = false
    },

    // 新增按钮
    async handleAdd() {
      this.open = true
      this.title = '新增车型'
      this.formDlog.name = ''
      this.formDlog.allowableLoad = ''
      this.formDlog.allowableVolume = ''
      this.formDlog.measureHigh = '0.0'
      this.formDlog.measureLong = '0.0'
      this.formDlog.measureWidth = '0.0'
    },

    /* 编辑按钮 */
    async  handleUpdate(row) {
      console.log(row)
      this.open = true
      this.title = '编辑车型'
      this.formDlog = row
    },

    // 确定 按钮
    async submit() {
      this.$refs.form.validate()
      if (this.formDlog.id) {
        await updataCarType(this.formDlog.id, this.formDlog)
        this.$message.success('修改成功')
        // this.formDlog.id = ''
      } else {
        await addCarType(this.formDlog)
        this.$message.success('添加成功')
      }
      this.getCarList()
      this.cancel()
    },

    // 取消 按钮
    cancel() {
      // 新增和编辑公用表单 , 需要将表单重置为初始化的状态(与data中定义的保持一致)
      this.formDlog = {
        name: '',
        allowableLoad: '',
        allowableVolume: '',
        measureLong: '',
        measureWidth: '',
        measureHigh: ''
      }
      // 关闭弹框需要重置表单
      this.$refs.form.resetFields()
      this.open = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.carSearchFormData.pageSize = val
      this.getCarList()
    },
    handleCurrentChange(val) {
      this.carSearchFormData.page = val
      this.getCarList()
    },

    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除车型?', '删除车型', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        console.log(row.id)
        return delCarType(row.id)
      }).then(() => {
        this.getCarList()
        this.$message.success('删除成功')
      })
    },
    // 搜索
    search() {
      this.carSearchFormData.page = 1
      this.getCarList()
    },
    // 重置
    resetForm() {
      this.$refs.formData.resetFields()
      this.getCarList()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin-bottom: 20px;
}
.button{
  width:86px;
}
 .el-table{
  margin-top: 10px;
  border:1px solid #ebeef5
}
.el-pagination{
  font-size:18px;
  margin: 20px;
}
.dialog-footer{
  margin-top: 35px;
  margin-left:200px;
  .el-button{
    width:85px;
  }
}

 ::v-deep .el-dialog{
    overflow: hidden;
    border-radius: 10px;
     .el-dialog__header{
    background-color: #f3f4f7;
  }
   }
.dw{
    font-style:normal;
		color: #000000;
		line-height: 45px;
    padding-right:10px
  }
::v-deep label{
  font-size:14px;
  font-weight: normal;
  color: #000000;
}
.line {
  display: inline-block;
  width: 1px;
  height: 1em;
  margin: 0 8px;
  vertical-align: middle;
  background-color: #dcdfe6;
}

</style>

